package com.woniuxy.vuecli.service.impl;

import com.woniuxy.vuecli.entity.Role;
import com.woniuxy.vuecli.mapper.RoleMapper;
import com.woniuxy.vuecli.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author author
 * @Create 2024-03-06 9:35
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;

    @Override
    public List<Role> getAll() {
        return roleMapper.selectAll();
    }

    //事务定义，应用场景
    //事务的特性 ACID
    //I隔离性：四种隔离级别 MySQL的默认隔离级别是，可重复
    //传播机制：7种，required
    //JDBC控制:同一个连接
//            Connection  connection;
//           try{
//                connection.setAutoCommit(false);
//                //调用多次数据库添加，删除，修改
//                connection.commit()
//             } catch(){
//                connection.rollback()
//             }
    //Spring事务管理原理：AOP
    //事务失效
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    @Override
    public void addMenu(int rid, int[] mids) {
        roleMapper.deleteMenusByRid(rid);
        //模拟出错
//        if (true) {
//            throw new RuntimeException("分配菜单异常");
//        }
        roleMapper.insertMenusByRid(rid, mids);
    }
}
